/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IInfoResult;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 info' command.
  * 
  * @version $Revision: 67 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Info extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the operation's result. */
	private Result result = new Result();	
	/** Stores the progress monitor that should be used. */
	private IProgressMonitor monitor;
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Info.
	  * 
	  * @param monitor the progress monitor.
	  * @return the new Info instance.
	  */
	public static final Info newInstance(IProgressMonitor monitor)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Info(status, monitor); 
	}

	// ------------------------------------------------------------------------ Private Constructors

	/** Constructor for Info.
	  * 
	  * @param status the MultiStatus instance.
	  * @param monitor the progress monitor.
	  */
	private Info(MultiStatus status, IProgressMonitor monitor)
	{
		super(status);
		this.monitor = monitor;
	}	

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputInfo(char, String) */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
		{
			return;
		}
		
		if (infoString.startsWith("User name:"))                                       //$NON-NLS-1$
		{
			result.setUserName(infoString.substring(11));
		}
		else
		if (infoString.startsWith("Client name:"))                                     //$NON-NLS-1$
		{
			result.setClientName(infoString.substring(13));
		}
		else
		if (infoString.startsWith("Client host:"))                                     //$NON-NLS-1$
		{
			result.setClientHost(infoString.substring(13));
		}
		else
		if (infoString.startsWith("Client root:"))                                     //$NON-NLS-1$
		{
			result.setClientRoot(infoString.substring(13));
		}
		else
		if (infoString.startsWith("Client address:"))                                  //$NON-NLS-1$
		{
			result.setClientAddress(infoString.substring(16));
		}
		else
		if (infoString.startsWith("Server address:"))                                  //$NON-NLS-1$
		{
			result.setServerAddress(infoString.substring(16));
		}
		else
		if (infoString.startsWith("Server root:"))                                     //$NON-NLS-1$
		{
			result.setServerRoot(infoString.substring(13));
		}
		else
		if (infoString.startsWith("Server date:"))                                     //$NON-NLS-1$
		{
			result.setServerDate(infoString.substring(13));
		}
		else
		if (infoString.startsWith("Server version:"))                                  //$NON-NLS-1$
		{
			result.setServerVersion(infoString.substring(16));
		}
		else
		if (infoString.startsWith("Server license:"))                                  //$NON-NLS-1$
		{
			result.setServerLicense(infoString.substring(16));
		}
		
		monitor.worked(1);
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "info";                                                                 //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus() */	 
	public IStatus getStatus()
	{
		return getMultiStatus();
	}	

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	
		
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return the operations result.
	  */
	public IInfoResult getResult()
	{
		return result;
	}

	// ---------------------------------------------------------------------- Private Nested Classes
	
	/**
	  * Implementation of the IInfoResult. Stores information provided
	  * by the 'p4 info' command. This is just a dumb container.
	  * 
	  * @see IInfoResult
	  */
	private static class Result implements IInfoResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the user name. */
		private String userName;
		/** Stores the client name. */
		private String clientName;
		/** Stores the client host. */
		private String clientHost;
		/** Stores the client root. */
		private String clientRoot;
		/** Stores the client address. */
		private String clientAddress;
		/** Stores the server address. */
		private String serverAddress;
		/** Stores the server root. */
		private String serverRoot;
		/** Stores the server date. */
		private String serverDate;
		/** Stores the server version. */
		private String serverVersion;
		/** Stores the server license. */
		private String serverLicense;
	
		// -------------------------------------------------------------------------- Public Methods
	
		/** @see IInfoResult#getUserName() */
		public String getUserName() 
		{ 
			return userName;
		}
	
		/** @see IInfoResult#getClientName() */
		public String getClientName() 
		{ 
			return clientName; 
		}
	
		/** @see IInfoResult#getClientHost() */
		public String getClientHost() 
		{ 
			return clientHost; 
		}
	
		/** @see IInfoResult#getClientRoot() */
		public String getClientRoot() 
		{ 
			return clientRoot; 
		}
			
		/** @see IInfoResult#getClientAddress() */
		public String getClientAddress() 
		{ 
			return clientAddress; 
		}
	
		/** @see IInfoResult#getServerAddress() */
		public String getServerAddress() 
		{ 
			return serverAddress; 
		}
	
		/** @see IInfoResult#getServerRoot() */
		public String getServerRoot() 
		{ 
			return serverRoot; 
		}
	
		/** @see IInfoResult#getServerDate() */
		public String getServerDate() 
		{ 
			return serverDate; 
		}
	
		/** @see IInfoResult#getServerVersion() */
		public String getServerVersion() 
		{ 
			return serverVersion; 
		}
	
		/** @see IInfoResult#getServerLicense() */
		public String getServerLicense() 
		{ 
			return serverLicense; 
		}
		
		// --------------------------------------------------------------------------------- Methods

		/** Sets the clientAddress.
		  * 
		  * @param clientAddress The clientAddress to set
		  */
		void setClientAddress(String clientAddress) 
		{
			this.clientAddress = clientAddress;
		}

		/** Sets the clientHost.
		  * 
		  * @param clientHost The clientHost to set
		  */
		void setClientHost(String clientHost) 
		{
			this.clientHost = clientHost;
		}

		/** Sets the clientName.
		  * 
		  * @param clientName The clientName to set
		  */
		void setClientName(String clientName) 
		{
			this.clientName = clientName;
		}

		/** Sets the clientRoot.
		  * 
		  * @param clientRoot The clientRoot to set
		  */
		void setClientRoot(String clientRoot) 
		{
			this.clientRoot = clientRoot;
		}

		/** Sets the serverAddress.
		  * 
		  * @param serverAddress The serverAddress to set
		  */
		void setServerAddress(String serverAddress) 
		{
			this.serverAddress = serverAddress;
		}

		/** Sets the serverDate.
		  * 
		  * @param serverDate The serverDate to set
		  */
		void setServerDate(String serverDate) 
		{
			this.serverDate = serverDate;
		}

		/** Sets the serverLicense.
		  * 
		  * @param serverLicense The serverLicense to set
		  */
		void setServerLicense(String serverLicense) 
		{
			this.serverLicense = serverLicense;
		}

		/** Sets the serverRoot.
		  * 
		  * @param serverRoot The serverRoot to set
		  */
		void setServerRoot(String serverRoot) 
		{
			this.serverRoot = serverRoot;
		}

		/** Sets the serverVersion.
		  * 
		  * @param serverVersion The serverVersion to set
		  */
		void setServerVersion(String serverVersion) 
		{
			this.serverVersion = serverVersion;
		}

		/** Sets the userName.
		  * 
		  * @param userName The userName to set
		  */
		void setUserName(String userName) 
		{
			this.userName = userName;
		}
	}		
}
